<?php

namespace app\common\model\blfyy;

use app\common\model\BaseModel;
use think\Db;
use chrome\ChromePhp;

//引入chromephp插件
use common\Common;
use think\Exception;

//公共函数

/**
 * 提供表单管理服务
 * @name LibStatistic.php
 * @package ysj
 * @category model
 * @link http://www.chanekeji.com
 * @author syben
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2021-03-11
 */
class LibStatistic extends BaseModel
{
    protected $m_statistic;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->m_statistic = db('blf_statistics');
        parent::__construct();
    }

    /**
     * 添加表单信息
     */
    public function addStatistic($condition)
    {
        try {
            $condition['add_time'] = Common::getTime();
            $reslut = $this->m_statistic->insertGetId($condition);
            if (true == $reslut) {
                return Common::errorArray(0, "添加成功", $reslut);
            } else {
                return Common::errorArray(1, "添加失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }


    /**
     * 查询单个表单信息
     * @param $condition
     * @param array $result
     */
    public function findStatistic($condition = '', $field = '')
    {
        try {
            $reslut = $this->m_statistic->where($condition)->field($field)->find();

            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 查询多个表单信息
     * @param $condition
     * @param $limit
     * @param $field
     * @param $sort
     * @param array $result
     */
    public function findAllStatistic($condition = '', $limit = '', $field = '', $sort = 'add_time desc')
    {
        try {
            $reslut = $this->m_statistic->where($condition)->order($sort)->limit($limit)->field($field)->select();
            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 统计类别数量
     */
    function getAllStatistic($conditionList)
    {
        try {
            if (!empty($conditionList)) {
                if (count($conditionList) > 0) {
                    $whereString = "";
                    foreach ($conditionList as $condition) {
                        $whereString .= " {$condition['field']} {$condition['operator']}" . self::isNumberic($condition['value']) . " AND";
                    }
                }
                $whereString = rtrim($whereString, "AND");
                $sql_1 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 1";
                $sql_2 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 2";
                $sql_3 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 3";
                $sql_4 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 4";
                $sql_5 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 5";
                $sql_6 = "select type,count(*) as num from blf_statistics where {$whereString} and type = 6";
                $result_1 = Db::query($sql_1);
                $result_2 = Db::query($sql_2);
                $result_3 = Db::query($sql_3);
                $result_4 = Db::query($sql_4);
                $result_5 = Db::query($sql_5);
                $result_6 = Db::query($sql_6);
                $result = [];
                array_push($result, $result_1[0], $result_2[0], $result_3[0], $result_4[0], $result_5[0], $result_6[0]);

            } else {
                $sql = "select type,count(*) as num from blf_statistics group by type";
                $result = Db::query($sql);
            }
            if ($result) {
                return Common::errorArray(0, "统计成功", $result);
            } else {
                return Common::errorArray(0, "统计失败", $result);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }

    }

    /*
     * 判断字符串是否为数字
     */
    static function isNumberic($val)
    {
        if (is_numeric($val)) {
            $string = $val;
        } else {
            $string = "'" . $val . "'";
        }
        return $string;
    }

    /**
     * 更新表单信息
     * @param $condition
     * @param $userInfo
     * @param array $result
     */
    public function updateStatistic($condition, $userInfo)
    {
        try {
            $reslut = $this->m_statistic->where($condition)->update($userInfo);
            if (true == $reslut) {
                return Common::errorArray(0, "更新成功", $reslut);
            } else {
                return Common::errorArray(1, "更新失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 删除表单信息
     * @param $condition
     * @param array $result
     */
    public function deleteStatistic($ids)
    {
        $condition = array('id' => array('in', $ids));
        try {
            $reslut = $this->m_statistic->where($condition)->delete();;
            if (true == $reslut) {
                return Common::errorArray(0, "删除成功", $reslut);
            } else {
                return Common::errorArray(1, "删除失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 字段值加1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function increaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->m_statistic->where($condition)->setInc($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 字段值减1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function decreaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->m_statistic->where($condition)->setDec($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 分页获取数据表单信息
     */
    public function pagingStatictic($page, $conditionList, $sort = '', $orList = null)
    {
        $reslut = Common::paging('blf_statistics', $page, $conditionList, $sort, $orList);
        return $reslut;
    }

    /**
     * 获取销售关键指标
     * @return array $result
     */
    function getKeyIndexOfSale()
    {
        $curDate = date("Y-m-d", time());
        $lowDate = "{$curDate} 00:00:00";
        $upDate = "{$curDate} 23:59:59";
        try {
            $sql1 = <<<EOF
SELECT COUNT(*) AS payCount
FROM `ysj_project_order` `a` 
WHERE `a`.`state` = 2 AND `a`.`add_time` >= '{$lowDate}' AND `a`.`add_time` <= '{$upDate}'
EOF;
            $count1 = Db::query($sql1);
            $sql2 = <<<EOF
SELECT COUNT(*) AS orderCount
FROM `ysj_project_order` `a` 
WHERE `a`.`state` = 3 AND `a`.`add_time` >= '{$lowDate}' AND `a`.`add_time` <= '{$upDate}'
EOF;
            $count2 = Db::query($sql2);
            $sql3 = <<<EOF
SELECT COUNT(*) AS cancelCount
FROM `ysj_project_order` `a` 
WHERE `a`.`state` = 4 AND `a`.`add_time` >= '{$lowDate}' AND `a`.`add_time` <= '{$upDate}'
EOF;
            $count3 = Db::query($sql3);
            return Common::errorArray(0, "查找成功", [
                $count1[0]['payCount'],
                $count2[0]['orderCount'],
                $count3[0]['cancelCount']
            ]);
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 按日期获取订单量变化数据
     * @return array
     */
    function getQuantityData($from, $to)
    {
        try {
            $sql =
                <<<EOF
SELECT SUM(pay_amount) AS amount
FROM `ysj_project_order` `a` 
WHERE `a`.`add_time` >= '{$from}' AND `a`.`add_time` <= '{$to}'
EOF;
            $result = Db::query($sql);
            return Common::errorArray(0, "查找成功", is_null($result[0]['amount']) ? 0 : $result[0]['amount']);
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }

    /**
     * 按日期获取订单量变化数据
     * @return array
     */
    function getQuantityData2($from, $to)
    {
        try {
            $sql =
                <<<EOF
SELECT COUNT(*)  num
FROM `base_user` `a` 
WHERE `a`.`add_time` >= '{$from}' AND `a`.`add_time` <= '{$to}'
EOF;
            $result = Db::query($sql);
            return Common::errorArray(0, "查找成功", is_null($result[0]['num']) ? 0 : $result[0]['num']);
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败", $ex);
        }
    }
}


?>